{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6c7237b0-a878-49e9-ae70-87807e1a2d8b",
   "metadata": {},
   "source": [
    "# Pandas时间序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3b153cbe-6c5b-44d7-b730-f6c70570297c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2118500d-d71d-4c46-b164-c7478795504c",
   "metadata": {},
   "source": [
    "**时间戳**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6a59d23a-5ccc-44ba-9f98-c914f8d38bb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-02-13    5\n",
       "2030-02-14    9\n",
       "2030-02-15    8\n",
       "2030-02-16    4\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.创建时间戳\n",
    "pd.Timestamp('2030-2-3') # 时刻数据\n",
    "# freq时间显示位置默认D\n",
    "# Y：年，M：月，D：日\n",
    "pd.Period('2030-2-3',freq='D') # 时期数据\n",
    "\n",
    "# 批量创建时刻数据\n",
    "# periods：表示创建多少个时间\n",
    "# freq按天为周期\n",
    "index = pd.date_range('2030.02.13',periods=4,freq='D')# 时刻数据\n",
    "# index = pd.period_range('2030.02.13',periods=4,freq='D')# 时期数据\n",
    "index\n",
    "\n",
    "# 时间戳索引\n",
    "pd.Series(np.random.randint(0,10,size=4),index=index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "443dde67-cbc8-4c0a-9020-60c95960be31",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\1819398687.py:4: UserWarning: Parsing dates in %d/%m/%Y format when dayfirst=False (the default) was specified. Pass `dayfirst=True` or specify a format to silence this warning.\n",
      "  pd.to_datetime(['14/03/2030','05/03/2030'])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-14 23:41:18.987000'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.转换方法\n",
    "pd.to_datetime(['2030.03.04','2030.03.05'])\n",
    "pd.to_datetime(['2030-3-14','2030-03-05'])\n",
    "pd.to_datetime(['14/03/2030','05/03/2030'])\n",
    "pd.to_datetime(['2030/3/14','2030/3/15'])\n",
    "\n",
    "# 时间戳转换为时间\n",
    "# unit时间戳单位默认为ms毫秒\n",
    "pd.to_datetime([1899678987],unit='s')\n",
    "dt=pd.to_datetime([1899678987],unit='ms')\n",
    "dt\n",
    "\n",
    "# 时间差DataOffset\n",
    "dt + pd.DateOffset(hours=8)# +8小时\n",
    "dt + pd.DateOffset(days=8)# +8天\n",
    "dt - pd.DateOffset(days=8)# -8天 \n",
    "dt + pd.DateOffset(days=-8)# -8天 \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "985cd61b-5fac-45e0-8a3a-e367b675cdf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-14     0\n",
       "2030-03-15     1\n",
       "2030-03-16     2\n",
       "2030-03-17     3\n",
       "2030-03-18     4\n",
       "              ..\n",
       "2030-06-17    95\n",
       "2030-06-18    96\n",
       "2030-06-19    97\n",
       "2030-06-20    98\n",
       "2030-06-21    99\n",
       "Freq: D, Length: 100, dtype: int64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.时间戳的索引和切片\n",
    "index = pd.date_range('2030-3-14',periods=100,freq='D')\n",
    "index\n",
    "\n",
    "ts = pd.Series(range(len(index)),index=index)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "185fce65-b570-4e8b-b534-3f8620619d0b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-15    1\n",
       "2030-03-16    2\n",
       "2030-03-17    3\n",
       "2030-03-18    4\n",
       "2030-03-19    5\n",
       "2030-03-20    6\n",
       "2030-03-21    7\n",
       "2030-03-22    8\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引\n",
    "ts['2030-03-15']\n",
    "\n",
    "#获取3月份所有值\n",
    "ts['2030-03']\n",
    "\n",
    "#获取2030年所有值\n",
    "ts['2030']\n",
    "\n",
    "#切片\n",
    "ts['2030-03-15':'2030-03-22']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "a9e2533c-6c30-46c7-b1ef-1f4b961bdfb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-24    10\n",
       "2030-03-25    11\n",
       "2030-03-26    12\n",
       "2030-03-27    13\n",
       "2030-03-28    14\n",
       "2030-03-29    15\n",
       "2030-03-30    16\n",
       "2030-03-31    17\n",
       "2030-04-01    18\n",
       "2030-04-02    19\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳索引\n",
    "pd.Timestamp('2030-3-22')\n",
    "ts[pd.Timestamp('2030-3-15'):pd.Timestamp('2030-3-22')]\n",
    "\n",
    "# date_range\n",
    "ts[pd.date_range('2030-3-24',periods=10,freq='D')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "55d75853-a5c9-42f2-844e-d39f33bf69e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5,\n",
       "       6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1,\n",
       "       2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,\n",
       "       5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0,\n",
       "       1, 2, 3, 4],\n",
       "      dtype='int32')"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.属性\n",
    "ts.index\n",
    "\n",
    "ts.index.year #取出所有的年\n",
    "ts.index.month #取出所有的月\n",
    "ts.index.day #取出所有的日\n",
    "ts.index.dayofweek #取出所有的星期"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d210a025-08f3-4f5e-9054-f55a494b1d8f",
   "metadata": {},
   "source": [
    "**时间序常用方法**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61d87f0b-316d-46f0-858a-38f1d75695de",
   "metadata": {},
   "source": [
    "- 对时间做一些移动/滞后、频率转换、采样等相关操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "241280aa-cbf5-4567-b18f-2dabc2f00027",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01    243\n",
       "2030-03-02    160\n",
       "2030-03-03    426\n",
       "2030-03-04    442\n",
       "2030-03-05    101\n",
       "             ... \n",
       "2031-02-24     80\n",
       "2031-02-25    183\n",
       "2031-02-26    347\n",
       "2031-02-27    200\n",
       "2031-02-28    310\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.date_range('2030-3-1',periods=365,freq='D')\n",
    "ts = pd.Series(np.random.randint(0,500,len(index)),index=index)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "5981f831-001b-42ba-92c5-e72d09d65710",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01    426.0\n",
       "2030-03-02    442.0\n",
       "2030-03-03    101.0\n",
       "2030-03-04     84.0\n",
       "2030-03-05    403.0\n",
       "              ...  \n",
       "2031-02-24    347.0\n",
       "2031-02-25    200.0\n",
       "2031-02-26    310.0\n",
       "2031-02-27      NaN\n",
       "2031-02-28      NaN\n",
       "Freq: D, Length: 365, dtype: float64"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.移动\n",
    "ts.shift()# 默认后移一位数据\n",
    "ts.shift(periods=2)# 后移两位数据\n",
    "ts.shift(periods=-2)# 前移两位数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "838ee275-43bf-4120-a33b-018fb68eb259",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01 00:00:00    243\n",
       "2030-03-01 01:00:00      0\n",
       "2030-03-01 02:00:00      0\n",
       "2030-03-01 03:00:00      0\n",
       "2030-03-01 04:00:00      0\n",
       "                      ... \n",
       "2031-02-27 20:00:00      0\n",
       "2031-02-27 21:00:00      0\n",
       "2031-02-27 22:00:00      0\n",
       "2031-02-27 23:00:00      0\n",
       "2031-02-28 00:00:00    310\n",
       "Freq: h, Length: 8737, dtype: int32"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.频率转换\n",
    "ts.asfreq(pd.tseries.offsets.Week()) #由每天转换为每周\n",
    "ts.asfreq(pd.tseries.offsets.MonthEnd()) #由每天转换为每月\n",
    "\n",
    "# 由少变多:fill_value填充\n",
    "ts.asfreq(pd.tseries.offsets.Hour(),fill_value=0) #由每天转换为每月"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c971674b-5110-459a-a793-2dbb4c936be1",
   "metadata": {},
   "source": [
    "**resample:根据日期维度进行聚合**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf5b16e1-efc1-48a0-be7a-ddbbf83fbdb3",
   "metadata": {},
   "source": [
    "- 按分钟(T)、小时(H)、日(D)、周(W)、月(M)、年(Y)等来作为日期维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "16741b8e-bded-4ae9-be64-4cdad34916c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01    243\n",
       "2030-03-02    160\n",
       "2030-03-03    426\n",
       "2030-03-04    442\n",
       "2030-03-05    101\n",
       "             ... \n",
       "2031-02-24     80\n",
       "2031-02-25    183\n",
       "2031-02-26    347\n",
       "2031-02-27    200\n",
       "2031-02-28    310\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.重采样 resample\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "02153e90-18fa-4347-945c-f747bf1cfd20",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\3988985298.py:6: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.\n",
      "  ts.resample('3M').sum().cumsum() #以三个月单位进行汇总，求和，累加\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\3988985298.py:8: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead.\n",
      "  ts.resample('H').sum()# 以一个小时单位进行汇总，求和\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\3988985298.py:9: FutureWarning: 'T' is deprecated and will be removed in a future version, please use 'min' instead.\n",
      "  ts.resample('T').sum()# 以一个分钟单位进行汇总，求和\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2030-03-01 00:00:00    243\n",
       "2030-03-01 00:01:00      0\n",
       "2030-03-01 00:02:00      0\n",
       "2030-03-01 00:03:00      0\n",
       "2030-03-01 00:04:00      0\n",
       "                      ... \n",
       "2031-02-27 23:56:00      0\n",
       "2031-02-27 23:57:00      0\n",
       "2031-02-27 23:58:00      0\n",
       "2031-02-27 23:59:00      0\n",
       "2031-02-28 00:00:00    310\n",
       "Freq: min, Length: 524161, dtype: int32"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('D').sum()# 以一天单位进行汇总，求和\n",
    "ts.resample('2D').sum()# 以两天单位进行汇总，求和\n",
    "\n",
    "ts.resample('2W').sum()# 以两个星期单位进行汇总，求和\n",
    "\n",
    "ts.resample('3M').sum().cumsum() #以三个月单位进行汇总，求和，累加\n",
    "\n",
    "ts.resample('H').sum()# 以一个小时单位进行汇总，求和\n",
    "ts.resample('T').sum()# 以一个分钟单位进行汇总，求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "ff882e7b-c1f0-4fc4-9856-bb2ae587c31f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "      <th>score</th>\n",
       "      <th>week</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "      <td>2030-03-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>2030-03-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>2030-03-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>44</td>\n",
       "      <td>50</td>\n",
       "      <td>2030-03-24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>33</td>\n",
       "      <td>60</td>\n",
       "      <td>2030-03-31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>44</td>\n",
       "      <td>70</td>\n",
       "      <td>2030-04-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>55</td>\n",
       "      <td>80</td>\n",
       "      <td>2030-04-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>66</td>\n",
       "      <td>10</td>\n",
       "      <td>2030-04-21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   price  score       week\n",
       "0     10     40 2030-03-03\n",
       "1     11     30 2030-03-10\n",
       "2      2     20 2030-03-17\n",
       "3     44     50 2030-03-24\n",
       "4     33     60 2030-03-31\n",
       "5     44     70 2030-04-07\n",
       "6     55     80 2030-04-14\n",
       "7     66     10 2030-04-21"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.DataFrame重采样\n",
    "d={\n",
    "    'price':[10,11,2,44,33,44,55,66],\n",
    "    'score':[40,30,20,50,60,70,80,10],\n",
    "    'week':pd.date_range('2030-3-1',periods=8,freq='W')\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(d)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "41186a00-426f-4020-b2d0-f1afe819787f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\2066966880.py:2: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.\n",
      "  df.resample('M',on='week').sum()\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\2066966880.py:4: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.\n",
      "  df.resample('M',on='week').agg({'price':np.mean,'score':'sum'})\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_111580\\2066966880.py:4: FutureWarning: The provided callable <function mean at 0x000001C67E1F0680> is currently using SeriesGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  df.resample('M',on='week').agg({'price':np.mean,'score':'sum'})\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>week</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2030-03-31</th>\n",
       "      <td>20.0</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2030-04-30</th>\n",
       "      <td>55.0</td>\n",
       "      <td>160</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            price  score\n",
       "week                    \n",
       "2030-03-31   20.0    200\n",
       "2030-04-30   55.0    160"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对Week列进行按月汇总求和\n",
    "df.resample('M',on='week').sum()\n",
    "\n",
    "df.resample('M',on='week').agg({'price':np.mean,'score':'sum'})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06f6e6ee-f561-4d39-9a06-e40509b71d6b",
   "metadata": {},
   "source": [
    "**时区**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "4348e943-daf6-44bb-aa7d-63f9a1ac4010",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01    1\n",
       "2030-03-02    1\n",
       "2030-03-03    1\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.date_range('2030-3-1 00:00',periods=3,freq='D')\n",
    "ts = pd.Series(np.random.randint(len(index)),index=index)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "6632b22c-a945-4d0d-968e-5cf6ad0d3aec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tz:timezone 时区\n",
    "import pytz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "c4c85afd-3e3c-4982-a5bb-52de1f39e3df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Africa/Abidjan',\n",
       " 'Africa/Accra',\n",
       " 'Africa/Addis_Ababa',\n",
       " 'Africa/Algiers',\n",
       " 'Africa/Asmara',\n",
       " 'Africa/Bamako',\n",
       " 'Africa/Bangui',\n",
       " 'Africa/Banjul',\n",
       " 'Africa/Bissau',\n",
       " 'Africa/Blantyre',\n",
       " 'Africa/Brazzaville',\n",
       " 'Africa/Bujumbura',\n",
       " 'Africa/Cairo',\n",
       " 'Africa/Casablanca',\n",
       " 'Africa/Ceuta',\n",
       " 'Africa/Conakry',\n",
       " 'Africa/Dakar',\n",
       " 'Africa/Dar_es_Salaam',\n",
       " 'Africa/Djibouti',\n",
       " 'Africa/Douala',\n",
       " 'Africa/El_Aaiun',\n",
       " 'Africa/Freetown',\n",
       " 'Africa/Gaborone',\n",
       " 'Africa/Harare',\n",
       " 'Africa/Johannesburg',\n",
       " 'Africa/Juba',\n",
       " 'Africa/Kampala',\n",
       " 'Africa/Khartoum',\n",
       " 'Africa/Kigali',\n",
       " 'Africa/Kinshasa',\n",
       " 'Africa/Lagos',\n",
       " 'Africa/Libreville',\n",
       " 'Africa/Lome',\n",
       " 'Africa/Luanda',\n",
       " 'Africa/Lubumbashi',\n",
       " 'Africa/Lusaka',\n",
       " 'Africa/Malabo',\n",
       " 'Africa/Maputo',\n",
       " 'Africa/Maseru',\n",
       " 'Africa/Mbabane',\n",
       " 'Africa/Mogadishu',\n",
       " 'Africa/Monrovia',\n",
       " 'Africa/Nairobi',\n",
       " 'Africa/Ndjamena',\n",
       " 'Africa/Niamey',\n",
       " 'Africa/Nouakchott',\n",
       " 'Africa/Ouagadougou',\n",
       " 'Africa/Porto-Novo',\n",
       " 'Africa/Sao_Tome',\n",
       " 'Africa/Tripoli',\n",
       " 'Africa/Tunis',\n",
       " 'Africa/Windhoek',\n",
       " 'America/Adak',\n",
       " 'America/Anchorage',\n",
       " 'America/Anguilla',\n",
       " 'America/Antigua',\n",
       " 'America/Araguaina',\n",
       " 'America/Argentina/Buenos_Aires',\n",
       " 'America/Argentina/Catamarca',\n",
       " 'America/Argentina/Cordoba',\n",
       " 'America/Argentina/Jujuy',\n",
       " 'America/Argentina/La_Rioja',\n",
       " 'America/Argentina/Mendoza',\n",
       " 'America/Argentina/Rio_Gallegos',\n",
       " 'America/Argentina/Salta',\n",
       " 'America/Argentina/San_Juan',\n",
       " 'America/Argentina/San_Luis',\n",
       " 'America/Argentina/Tucuman',\n",
       " 'America/Argentina/Ushuaia',\n",
       " 'America/Aruba',\n",
       " 'America/Asuncion',\n",
       " 'America/Atikokan',\n",
       " 'America/Bahia',\n",
       " 'America/Bahia_Banderas',\n",
       " 'America/Barbados',\n",
       " 'America/Belem',\n",
       " 'America/Belize',\n",
       " 'America/Blanc-Sablon',\n",
       " 'America/Boa_Vista',\n",
       " 'America/Bogota',\n",
       " 'America/Boise',\n",
       " 'America/Cambridge_Bay',\n",
       " 'America/Campo_Grande',\n",
       " 'America/Cancun',\n",
       " 'America/Caracas',\n",
       " 'America/Cayenne',\n",
       " 'America/Cayman',\n",
       " 'America/Chicago',\n",
       " 'America/Chihuahua',\n",
       " 'America/Ciudad_Juarez',\n",
       " 'America/Costa_Rica',\n",
       " 'America/Creston',\n",
       " 'America/Cuiaba',\n",
       " 'America/Curacao',\n",
       " 'America/Danmarkshavn',\n",
       " 'America/Dawson',\n",
       " 'America/Dawson_Creek',\n",
       " 'America/Denver',\n",
       " 'America/Detroit',\n",
       " 'America/Dominica',\n",
       " 'America/Edmonton',\n",
       " 'America/Eirunepe',\n",
       " 'America/El_Salvador',\n",
       " 'America/Fort_Nelson',\n",
       " 'America/Fortaleza',\n",
       " 'America/Glace_Bay',\n",
       " 'America/Goose_Bay',\n",
       " 'America/Grand_Turk',\n",
       " 'America/Grenada',\n",
       " 'America/Guadeloupe',\n",
       " 'America/Guatemala',\n",
       " 'America/Guayaquil',\n",
       " 'America/Guyana',\n",
       " 'America/Halifax',\n",
       " 'America/Havana',\n",
       " 'America/Hermosillo',\n",
       " 'America/Indiana/Indianapolis',\n",
       " 'America/Indiana/Knox',\n",
       " 'America/Indiana/Marengo',\n",
       " 'America/Indiana/Petersburg',\n",
       " 'America/Indiana/Tell_City',\n",
       " 'America/Indiana/Vevay',\n",
       " 'America/Indiana/Vincennes',\n",
       " 'America/Indiana/Winamac',\n",
       " 'America/Inuvik',\n",
       " 'America/Iqaluit',\n",
       " 'America/Jamaica',\n",
       " 'America/Juneau',\n",
       " 'America/Kentucky/Louisville',\n",
       " 'America/Kentucky/Monticello',\n",
       " 'America/Kralendijk',\n",
       " 'America/La_Paz',\n",
       " 'America/Lima',\n",
       " 'America/Los_Angeles',\n",
       " 'America/Lower_Princes',\n",
       " 'America/Maceio',\n",
       " 'America/Managua',\n",
       " 'America/Manaus',\n",
       " 'America/Marigot',\n",
       " 'America/Martinique',\n",
       " 'America/Matamoros',\n",
       " 'America/Mazatlan',\n",
       " 'America/Menominee',\n",
       " 'America/Merida',\n",
       " 'America/Metlakatla',\n",
       " 'America/Mexico_City',\n",
       " 'America/Miquelon',\n",
       " 'America/Moncton',\n",
       " 'America/Monterrey',\n",
       " 'America/Montevideo',\n",
       " 'America/Montserrat',\n",
       " 'America/Nassau',\n",
       " 'America/New_York',\n",
       " 'America/Nome',\n",
       " 'America/Noronha',\n",
       " 'America/North_Dakota/Beulah',\n",
       " 'America/North_Dakota/Center',\n",
       " 'America/North_Dakota/New_Salem',\n",
       " 'America/Nuuk',\n",
       " 'America/Ojinaga',\n",
       " 'America/Panama',\n",
       " 'America/Paramaribo',\n",
       " 'America/Phoenix',\n",
       " 'America/Port-au-Prince',\n",
       " 'America/Port_of_Spain',\n",
       " 'America/Porto_Velho',\n",
       " 'America/Puerto_Rico',\n",
       " 'America/Punta_Arenas',\n",
       " 'America/Rankin_Inlet',\n",
       " 'America/Recife',\n",
       " 'America/Regina',\n",
       " 'America/Resolute',\n",
       " 'America/Rio_Branco',\n",
       " 'America/Santarem',\n",
       " 'America/Santiago',\n",
       " 'America/Santo_Domingo',\n",
       " 'America/Sao_Paulo',\n",
       " 'America/Scoresbysund',\n",
       " 'America/Sitka',\n",
       " 'America/St_Barthelemy',\n",
       " 'America/St_Johns',\n",
       " 'America/St_Kitts',\n",
       " 'America/St_Lucia',\n",
       " 'America/St_Thomas',\n",
       " 'America/St_Vincent',\n",
       " 'America/Swift_Current',\n",
       " 'America/Tegucigalpa',\n",
       " 'America/Thule',\n",
       " 'America/Tijuana',\n",
       " 'America/Toronto',\n",
       " 'America/Tortola',\n",
       " 'America/Vancouver',\n",
       " 'America/Whitehorse',\n",
       " 'America/Winnipeg',\n",
       " 'America/Yakutat',\n",
       " 'Antarctica/Casey',\n",
       " 'Antarctica/Davis',\n",
       " 'Antarctica/DumontDUrville',\n",
       " 'Antarctica/Macquarie',\n",
       " 'Antarctica/Mawson',\n",
       " 'Antarctica/McMurdo',\n",
       " 'Antarctica/Palmer',\n",
       " 'Antarctica/Rothera',\n",
       " 'Antarctica/Syowa',\n",
       " 'Antarctica/Troll',\n",
       " 'Antarctica/Vostok',\n",
       " 'Arctic/Longyearbyen',\n",
       " 'Asia/Aden',\n",
       " 'Asia/Almaty',\n",
       " 'Asia/Amman',\n",
       " 'Asia/Anadyr',\n",
       " 'Asia/Aqtau',\n",
       " 'Asia/Aqtobe',\n",
       " 'Asia/Ashgabat',\n",
       " 'Asia/Atyrau',\n",
       " 'Asia/Baghdad',\n",
       " 'Asia/Bahrain',\n",
       " 'Asia/Baku',\n",
       " 'Asia/Bangkok',\n",
       " 'Asia/Barnaul',\n",
       " 'Asia/Beirut',\n",
       " 'Asia/Bishkek',\n",
       " 'Asia/Brunei',\n",
       " 'Asia/Chita',\n",
       " 'Asia/Choibalsan',\n",
       " 'Asia/Colombo',\n",
       " 'Asia/Damascus',\n",
       " 'Asia/Dhaka',\n",
       " 'Asia/Dili',\n",
       " 'Asia/Dubai',\n",
       " 'Asia/Dushanbe',\n",
       " 'Asia/Famagusta',\n",
       " 'Asia/Gaza',\n",
       " 'Asia/Hebron',\n",
       " 'Asia/Ho_Chi_Minh',\n",
       " 'Asia/Hong_Kong',\n",
       " 'Asia/Hovd',\n",
       " 'Asia/Irkutsk',\n",
       " 'Asia/Jakarta',\n",
       " 'Asia/Jayapura',\n",
       " 'Asia/Jerusalem',\n",
       " 'Asia/Kabul',\n",
       " 'Asia/Kamchatka',\n",
       " 'Asia/Karachi',\n",
       " 'Asia/Kathmandu',\n",
       " 'Asia/Khandyga',\n",
       " 'Asia/Kolkata',\n",
       " 'Asia/Krasnoyarsk',\n",
       " 'Asia/Kuala_Lumpur',\n",
       " 'Asia/Kuching',\n",
       " 'Asia/Kuwait',\n",
       " 'Asia/Macau',\n",
       " 'Asia/Magadan',\n",
       " 'Asia/Makassar',\n",
       " 'Asia/Manila',\n",
       " 'Asia/Muscat',\n",
       " 'Asia/Nicosia',\n",
       " 'Asia/Novokuznetsk',\n",
       " 'Asia/Novosibirsk',\n",
       " 'Asia/Omsk',\n",
       " 'Asia/Oral',\n",
       " 'Asia/Phnom_Penh',\n",
       " 'Asia/Pontianak',\n",
       " 'Asia/Pyongyang',\n",
       " 'Asia/Qatar',\n",
       " 'Asia/Qostanay',\n",
       " 'Asia/Qyzylorda',\n",
       " 'Asia/Riyadh',\n",
       " 'Asia/Sakhalin',\n",
       " 'Asia/Samarkand',\n",
       " 'Asia/Seoul',\n",
       " 'Asia/Shanghai',\n",
       " 'Asia/Singapore',\n",
       " 'Asia/Srednekolymsk',\n",
       " 'Asia/Taipei',\n",
       " 'Asia/Tashkent',\n",
       " 'Asia/Tbilisi',\n",
       " 'Asia/Tehran',\n",
       " 'Asia/Thimphu',\n",
       " 'Asia/Tokyo',\n",
       " 'Asia/Tomsk',\n",
       " 'Asia/Ulaanbaatar',\n",
       " 'Asia/Urumqi',\n",
       " 'Asia/Ust-Nera',\n",
       " 'Asia/Vientiane',\n",
       " 'Asia/Vladivostok',\n",
       " 'Asia/Yakutsk',\n",
       " 'Asia/Yangon',\n",
       " 'Asia/Yekaterinburg',\n",
       " 'Asia/Yerevan',\n",
       " 'Atlantic/Azores',\n",
       " 'Atlantic/Bermuda',\n",
       " 'Atlantic/Canary',\n",
       " 'Atlantic/Cape_Verde',\n",
       " 'Atlantic/Faroe',\n",
       " 'Atlantic/Madeira',\n",
       " 'Atlantic/Reykjavik',\n",
       " 'Atlantic/South_Georgia',\n",
       " 'Atlantic/St_Helena',\n",
       " 'Atlantic/Stanley',\n",
       " 'Australia/Adelaide',\n",
       " 'Australia/Brisbane',\n",
       " 'Australia/Broken_Hill',\n",
       " 'Australia/Darwin',\n",
       " 'Australia/Eucla',\n",
       " 'Australia/Hobart',\n",
       " 'Australia/Lindeman',\n",
       " 'Australia/Lord_Howe',\n",
       " 'Australia/Melbourne',\n",
       " 'Australia/Perth',\n",
       " 'Australia/Sydney',\n",
       " 'Canada/Atlantic',\n",
       " 'Canada/Central',\n",
       " 'Canada/Eastern',\n",
       " 'Canada/Mountain',\n",
       " 'Canada/Newfoundland',\n",
       " 'Canada/Pacific',\n",
       " 'Europe/Amsterdam',\n",
       " 'Europe/Andorra',\n",
       " 'Europe/Astrakhan',\n",
       " 'Europe/Athens',\n",
       " 'Europe/Belgrade',\n",
       " 'Europe/Berlin',\n",
       " 'Europe/Bratislava',\n",
       " 'Europe/Brussels',\n",
       " 'Europe/Bucharest',\n",
       " 'Europe/Budapest',\n",
       " 'Europe/Busingen',\n",
       " 'Europe/Chisinau',\n",
       " 'Europe/Copenhagen',\n",
       " 'Europe/Dublin',\n",
       " 'Europe/Gibraltar',\n",
       " 'Europe/Guernsey',\n",
       " 'Europe/Helsinki',\n",
       " 'Europe/Isle_of_Man',\n",
       " 'Europe/Istanbul',\n",
       " 'Europe/Jersey',\n",
       " 'Europe/Kaliningrad',\n",
       " 'Europe/Kirov',\n",
       " 'Europe/Kyiv',\n",
       " 'Europe/Lisbon',\n",
       " 'Europe/Ljubljana',\n",
       " 'Europe/London',\n",
       " 'Europe/Luxembourg',\n",
       " 'Europe/Madrid',\n",
       " 'Europe/Malta',\n",
       " 'Europe/Mariehamn',\n",
       " 'Europe/Minsk',\n",
       " 'Europe/Monaco',\n",
       " 'Europe/Moscow',\n",
       " 'Europe/Oslo',\n",
       " 'Europe/Paris',\n",
       " 'Europe/Podgorica',\n",
       " 'Europe/Prague',\n",
       " 'Europe/Riga',\n",
       " 'Europe/Rome',\n",
       " 'Europe/Samara',\n",
       " 'Europe/San_Marino',\n",
       " 'Europe/Sarajevo',\n",
       " 'Europe/Saratov',\n",
       " 'Europe/Simferopol',\n",
       " 'Europe/Skopje',\n",
       " 'Europe/Sofia',\n",
       " 'Europe/Stockholm',\n",
       " 'Europe/Tallinn',\n",
       " 'Europe/Tirane',\n",
       " 'Europe/Ulyanovsk',\n",
       " 'Europe/Vaduz',\n",
       " 'Europe/Vatican',\n",
       " 'Europe/Vienna',\n",
       " 'Europe/Vilnius',\n",
       " 'Europe/Volgograd',\n",
       " 'Europe/Warsaw',\n",
       " 'Europe/Zagreb',\n",
       " 'Europe/Zurich',\n",
       " 'GMT',\n",
       " 'Indian/Antananarivo',\n",
       " 'Indian/Chagos',\n",
       " 'Indian/Christmas',\n",
       " 'Indian/Cocos',\n",
       " 'Indian/Comoro',\n",
       " 'Indian/Kerguelen',\n",
       " 'Indian/Mahe',\n",
       " 'Indian/Maldives',\n",
       " 'Indian/Mauritius',\n",
       " 'Indian/Mayotte',\n",
       " 'Indian/Reunion',\n",
       " 'Pacific/Apia',\n",
       " 'Pacific/Auckland',\n",
       " 'Pacific/Bougainville',\n",
       " 'Pacific/Chatham',\n",
       " 'Pacific/Chuuk',\n",
       " 'Pacific/Easter',\n",
       " 'Pacific/Efate',\n",
       " 'Pacific/Fakaofo',\n",
       " 'Pacific/Fiji',\n",
       " 'Pacific/Funafuti',\n",
       " 'Pacific/Galapagos',\n",
       " 'Pacific/Gambier',\n",
       " 'Pacific/Guadalcanal',\n",
       " 'Pacific/Guam',\n",
       " 'Pacific/Honolulu',\n",
       " 'Pacific/Kanton',\n",
       " 'Pacific/Kiritimati',\n",
       " 'Pacific/Kosrae',\n",
       " 'Pacific/Kwajalein',\n",
       " 'Pacific/Majuro',\n",
       " 'Pacific/Marquesas',\n",
       " 'Pacific/Midway',\n",
       " 'Pacific/Nauru',\n",
       " 'Pacific/Niue',\n",
       " 'Pacific/Norfolk',\n",
       " 'Pacific/Noumea',\n",
       " 'Pacific/Pago_Pago',\n",
       " 'Pacific/Palau',\n",
       " 'Pacific/Pitcairn',\n",
       " 'Pacific/Pohnpei',\n",
       " 'Pacific/Port_Moresby',\n",
       " 'Pacific/Rarotonga',\n",
       " 'Pacific/Saipan',\n",
       " 'Pacific/Tahiti',\n",
       " 'Pacific/Tarawa',\n",
       " 'Pacific/Tongatapu',\n",
       " 'Pacific/Wake',\n",
       " 'Pacific/Wallis',\n",
       " 'US/Alaska',\n",
       " 'US/Arizona',\n",
       " 'US/Central',\n",
       " 'US/Eastern',\n",
       " 'US/Hawaii',\n",
       " 'US/Mountain',\n",
       " 'US/Pacific',\n",
       " 'UTC']"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 常用时区\n",
    "pytz.common_timezones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "f01bcbe8-88dc-4a7e-80c5-3ad1be4db016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01 00:00:00+00:00    1\n",
       "2030-03-02 00:00:00+00:00    1\n",
       "2030-03-03 00:00:00+00:00    1\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时区表示\n",
    "ts = ts.tz_localize(tz='UTC')\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "0a1c580f-4ac7-4083-87fa-88bfae219092",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01 08:00:00+08:00    1\n",
       "2030-03-02 08:00:00+08:00    1\n",
       "2030-03-03 08:00:00+08:00    1\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时区转换\n",
    "ts.tz_convert(tz='Asia/Shanghai')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
